package org.zjvis.graph.analysis.service.http;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zjvis.datascience.common.dto.user.UserDTO;
import org.zjvis.graph.analysis.service.exception.BaseErrorCode;
import org.zjvis.graph.analysis.service.exception.GraphAnalysisException;

@Service
public class UserService {
    @Autowired
    private RestTemplateUtil restTemplateUtil;

    public long getCurrentUserId() {
        UserDTO user = getCurrentUserDTO();
        return user.getId();
    }

    public UserDTO getCurrentUserDTO() {
        JSONObject resp = restTemplateUtil.post("/usersApi/getCurrentUserDTO", new JSONObject());
        UserDTO user = resp.getObject("result", UserDTO.class);
        if (user == null) {
            throw new GraphAnalysisException(BaseErrorCode.COOKIE_INVALID);
        }
        return user;
    }
}
